/**
 * 功能描述 导航栏
 */

define(function (require, exports, module) {
	var Subnav = require("subnav");
	var enmu = require("enums");
	var aw = require("ajaxwrapper");
	var store =require("store");
	var activeUser = store.get("user");	
	var intentionRegisterSubnav_Events = require("../handler/IntentionRegisterSubnav_Events");
	var intentionregister_Props = require("../constant/intentionregister_Props");
	
    var instance = {
        initComponent: function (params, widget) {
        	var subnav=new Subnav({
        		parentNode:".J-subnav",
				model : {
					title:"意向登记",
					items : [{
						id : "search",
						type : "search",
						placeholder:"客户",
						handler : function(str){
							var g = widget.get("grid");
							var obj = {
									str:str,
									"recordType" : "Visit",
									"saleConsultant":activeUser.pkUser,
									"customer.orderString":"visitTime:desc",
									fetchProperties:intentionregister_Props.gridFetchProperties
							};
							aw.ajax({
								url:"api/customervisitrecord/searchall",
								data:obj,
								dataType:"json",
								success:function(data){
									g.setData(data);
								}
							});
						}
					},{
					      id:"product",
					      type : "buttongroup",
					      all:{
					    	  show:false,
					    	  first:true
					      },
					      lazy:true,
					      tip:"产品",
					      keyField :"pkProduct",
					      valueField :"name",
					      params: function(){
					    	  return{
					    		  fetchProperties:"pkProduct,name"
					    	  }
					      },
					      url:"api/product/query",
					      handler:function(key,element){
					    	  intentionRegisterSubnav_Events.changeType(widget);
					      }
					},{
					      id:"productType",
					      type:"buttongroup",
					      all:{
					    	  show:false,
					    	  first:true
					      },
					      lazy:true,
					      tip:"产品类型",
					      keyField :"pkProductType",
					      valueField :"name",
					      handler:function(key,element){
					    	  widget.get("grid").refresh();
					      }
					},{
					      id:"intention",
					      tip:"意向",
					      type:"buttongroup",
						  all:{
							show:true,
							first:true
						  },
						  items:enmu["com.eling.elcms.travel.model.Intention"],
					      handler:function(key,element){
					    	  widget.get("grid").refresh();
					      }
					},{
					      id:"status",
					      type : "buttongroup",
					      all:{
					       show:true,
					       first:true
					      },
					      tip:"状态",
					      items:enmu["com.eling.elcms.travel.model.Customer.Status"],
					      handler:function(key,element){
					    	  widget.get("grid").refresh();
					      }
					}, {
			            id: "visitTime",
						tip:"最新来访时间",
						type:"daterange",
						ranges : {
							"今天": [moment().startOf("days"),moment().endOf("days")],
					        "本月": [moment().startOf("month"), moment().endOf("month")],
					        "本年": [moment().startOf("year"), moment().endOf("year")]
						},
						defaultRange : "本月",
						handler : function(time){
							 widget.get("grid").refresh();
						}
			        },{
						id : "add",
						type : "button",
						text:"新增",		
						handler:function(){
							widget.get("form").reset();
							widget.get("form").setValue("type", "Visit");
							widget.get("profile").reset();
							widget.$(".J-other-answer").val("");
							widget.show([".J-tab"]).hide([".J-grid"]);
							widget.get("subnav").hide(["add","visitTime","status","intention","productType","product","search"]).show(["return","save"]);							
						}
					},{
						id : "save",
						type : "button",
						text:"保存",						
						show : false,
						handler:function(){
							intentionRegisterSubnav_Events.save(widget);
						}
					},{
						id : "return",
						type : "button",
						text:"返回",						
						show : false,
						handler:function(){
							widget.show([".J-grid"]).hide([".J-tab"]);
							widget.get("subnav").show(["add","visitTime","status","intention","productType","product","search"]).hide(["return","save"]);							
							widget.get("grid").refresh();
						}
					}]
				}
        	});
        	widget.set("subnav",subnav);
        }
    };
    module.exports = instance;
});
